/*
 * @Date: 2022-12-21 12:52:06
 * @LastEditTime: 2023-09-22 15:09:13
 * 介绍:项目初始化
 */
import "@@/common/js/init";
import { unitPx } from "@@/utils/tools/css";
import { _require } from "@@/utils/tools";
import { isPc } from "@@/utils/tools/is";
import { setCssRootVar } from "./setCssRootVar";

window.isPc = isPc();

function resetRem() {
  const htmlDom = document.documentElement;
  let fontSize;
  if (isWidthModel(innerWidth) === "xs") {
    fontSize = 0.5;
    setCssRootVar({ "--gap-xxl": "32rem" });
  } else {
    fontSize = innerWidth / 1920;
    setCssRootVar({ "--gap-xxl": null });
  }
  return (htmlDom.style.fontSize = unitPx(fontSize));
}
export function isWidthModel(width: number): "xs" | "sm" | "md" | "lg" | "xl" {
  const mode = {
    sm: 576,
    md: 768,
    lg: 992,
    xl: 1200,
  };
  if (width > mode.xl) return "xl";
  if (width > mode.lg) return "lg";
  if (width > mode.md) return "md";
  if (width > mode.sm) return "sm";
  return "xs";
}

export function eventResetRem() {
  resetRem();
  return window.addEventListener("resize", resetRem);
}

export function preload() {
  const images: string[] = [];
  const backgrounds: string[] = [];

  backgrounds.forEach((v) => {
    const appendDiv = document.createElement("div");
    appendDiv.style.opacity = "0";
    appendDiv.style.position = "absolute";
    appendDiv.style.left = "-99999px";
    appendDiv.style.top = "-99999px";
    document.body.append(appendDiv);
    appendDiv.style.backgroundImage = `url(${_require(v)})`;
    setTimeout(() => appendDiv.remove());
  });
  images.forEach((v) => {
    new Image().src = _require(v);
  });
}
